from OCC.Core.BRepGProp import brepgprop_VolumeProperties
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeSolid
from OCC.Core.BRepGProp import brepgprop_VolumeProperties
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.GProp import GProp_GProps
from OCC.Core.TopAbs import TopAbs_SHELL, TopAbs_SOLID
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.TopoDS import topods
from OCC.Core.gp import gp_Trsf, gp_Vec
from OCC.Display.SimpleGui import init_display
from OCC.Extend.DataExchange import read_stl_file


def calc_volume(shape):
    props = GProp_GProps()
    brepgprop_VolumeProperties(shape, props)
    return props.Mass()


shapes = read_stl_file('./ceshi2.STL')

explorer = TopExp_Explorer(shapes, TopAbs_SHELL)

boxes = []

while explorer.More():
    shell = topods.Shell(explorer.Current())
    ms = BRepBuilderAPI_MakeSolid()
    ms.Add(shell)
    boxes.append(ms.Solid())
    explorer.Next()

print(boxes)
my_box = BRepPrimAPI_MakeBox(100, 100, 100).Shape()
my_box_transform = gp_Trsf()
my_box_transform.SetTranslation(gp_Vec(-50, -60, 20))
my_box.Location(TopLoc_Location(my_box_transform))
my_box_cut = BRepAlgoAPI_Cut(boxes[0], my_box)
my_box_result = my_box_cut.Shape()

rest_boxes = []
explorer2 = TopExp_Explorer(my_box_result, TopAbs_SOLID)
while explorer2.More():
    rest_boxes.append(explorer2.Current())
    explorer2.Next()

display, start_display, add_menu, add_function_to_menu = init_display()

# display.DisplayShape(shapes)

# display.DisplayShape(my_box_result)

for box in rest_boxes:
    display.DisplayShape(box)

start_display()
